package com.codi.api.controller.organ;

import com.codi.api.security.UserUtil;
import com.codi.base.exception.BaseAppException;
import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.StringUtil;
import com.codi.bus.core.resultModel.AssetsResult;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.resultModel.ShareListResult;
import com.codi.bus.core.service.organ.OrganLocalAssetsService;
import com.codi.message.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by asus on 2018/6/25.
 * 本地 资产
 */
@Slf4j
@RestController
@RequestMapping("/organ/localAssets")
public class OrganLocalAssetsController {
    @Autowired
    private OrganLocalAssetsService organLocalAssetsService;


    /**
     * 机构资产
     *
     * @return
     */
    @Loggable
    @GetMapping("/assets")
    @ResponseBody
    public BaseResult getOrganAssets() {
        AssetsResult assetsResult = new AssetsResult();
        Long profileId = UserUtil.getProfileId();
        Long userId = UserUtil.getLoginUserId();
        try {
            assetsResult = organLocalAssetsService.getOrganAssets(userId, profileId);
        } catch (Exception e) {
            e.printStackTrace();
            assetsResult.setSuccess(false);
        }
        return assetsResult;
    }

    /**
     * 企业宝资产
     *
     * @return
     * @throws BaseAppException
     */
    @Loggable
    @RequestMapping(value = "organBao", method = RequestMethod.GET)
    @ResponseBody
    public ShareListResult getOrganBaoAssets(Integer requestNum,
                                             Integer beginNum) throws BaseAppException {
        Long userId = UserUtil.getLoginUserId();
        Long pId = UserUtil.getProfileId();
        ShareListResult result = organLocalAssetsService.getAllOrganBao(userId, pId, requestNum, beginNum);
        return result;
    }

    /**
     * 基金资产
     *
     * @return
     * @throws BaseAppException
     */
    @Loggable
    @RequestMapping(value = "fund", method = RequestMethod.GET)
    @ResponseBody
    public ShareListResult getFundAssets(Integer requestNum,
                                         Integer beginNum) throws BaseAppException {
        Long userId = UserUtil.getLoginUserId();
        Long pId = UserUtil.getProfileId();
        ShareListResult result = organLocalAssetsService.getAllShares(userId, pId, requestNum, beginNum);
        return result;
    }

    /**
     * 查询单个基金资产
     * 企业宝和 普通基金共用
     */
    @RequestMapping(value = "/fundshare", method = RequestMethod.GET)
    @ResponseBody
    @Loggable
    public BaseResult shareQuery(String fundCode) {


        BaseResult result = null;
        try {
            result = organLocalAssetsService.getFundShares(UserUtil.getLoginUserId(), fundCode, UserUtil.getProfileId());
        } catch (BaseAppException e) {
            e.printStackTrace();
        }
        if (result.getSuccess() == false && StringUtil.isEmpty(result.getErrorMessage())) {
            result.setErrorMessage(MessageUtil.getErrorMessage(result.getErrorCode(), null));
        }

        return result;
    }
}
